﻿using System;
using System.ComponentModel.Composition;
using System.Linq;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;

namespace Songhay.OpenXml.Transformations
{
    using Songhay.Extensions;
    using Songhay.OpenXml;
    using Songhay.OpenXml.Extensions;
    using Songhay.OpenXml.Models;

    /// <summary>
    /// Transforms the Open XML WordML <c>sdt</c> element.
    /// </summary>
    [Export(typeof(IWordProcessingDocumentTransformation))]
    public class ContentControlForAmazonProductTransformation : IWordProcessingDocumentTransformation
    {
        /// <summary>
        /// Initializes a new instance
        /// of the <see cref="ContentControlForAmazonProductTransformation"/> class.
        /// </summary>
        public ContentControlForAmazonProductTransformation()
        {
            this.ExecutionOrdinal = 1;
        }

        #region IWordProcessingDocumentTransformation Members

        /// <summary>
        /// Gets or sets the execution ordinal.
        /// </summary>
        /// <value>The execution ordinal.</value>
        public byte ExecutionOrdinal { get; set; }

        /// <summary>
        /// Determines whether this instance can transform the specified word doc.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <returns>
        /// 	<c>true</c> if this instance can transform the specified word doc; otherwise, <c>false</c>.
        /// </returns>
        public bool CanTransform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node)
        {
            var element = node.GetElement(SonghayW.sdt);
            if(element == null) return false;
            if(!element.HasSdtTagName("rxAmazonProduct")) return false;

            var sdtDescendants = element
                .Element(SonghayW.sdtContent)
                .Descendants(SonghayW.sdt);

            return (sdtDescendants.Count() == 3);
        }

        /// <summary>
        /// Transforms the specified Word document.
        /// </summary>
        /// <param name="wordDoc">The Word document.</param>
        /// <param name="settings">The settings.</param>
        /// <param name="node">The node.</param>
        /// <param name="imageHandler">The image handler.</param>
        public object Transform(WordprocessingDocument wordDoc,
            SonghayHtmlConverterSettings settings, XNode node,
            Func<ImageInfo, XElement> imageHandler)
        {
            var sdt = node.GetElement(SonghayW.sdt);

            var sdtDescendants = sdt
                .Element(SonghayW.sdtContent)
                .Descendants(SonghayW.sdt);

            var productUri = "http://www.amazon.com/exec/obidos/ASIN/{0}/thekintespacec00A/";

            var src = ""; var style = "";

            sdtDescendants.ToList()
                .ForEach(
                    e =>
                    {
                        if(e.HasSdtTagName("rxAmazonProductAsin"))
                        {
                            var asin = e.GetSdtDisplayValue();
                            productUri = string.Format(productUri, asin);
                        }

                        if(e.HasSdtTagName("rxAmazonProductCss"))
                        {
                            style = e.GetSdtDisplayValue();
                        }

                        if(e.HasSdtTagName("rxAmazonProductImagePath"))
                        {
                            src = e.GetSdtDisplayValue();
                        }
                    });

            var a = new XElement(SonghayXhtml.a,
                new XAttribute(SonghayXhtml.href, productUri),
                new XAttribute(SonghayXhtml.title_attr, "Buy this product at Amazon.com!"),
                new XElement(SonghayXhtml.img,
                    new XAttribute(SonghayXhtml.alt, "Amazon.com product"),
                    new XAttribute(SonghayXhtml.src, src),
                    new XAttribute(SonghayXhtml.style_attr, style)
                )
            );

            return a;
        }

        #endregion
    }
}
